{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# User's Guide, Chapter 2: Notes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notated music, by its very name, consists of a bunch of notes that are put one after another or simultaneously on a staff. There are other things, clefs, key signatures, slurs, etc. but the heart of music is the notes; so to get anywhere in music21 you’ll need to know what the toolkit thinks about notes and how to work with them. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Go ahead and start IDLE or type “python” at the command line (Terminal on the Mac or “run: cmd” on Windows) and let’s get started."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating and working with Notes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The music21 concept of a standard note is contained in the :class:`~music21.note.Note` object, which is found in the\n",
    "`note` module. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Read this if you're new to Python** *(others can skip ahead):* Notice the \n",
    "difference between object names and\n",
    "module names.  Modules, which can contain one, many, or even\n",
    "zero objects, always begin with a lowercase letter.  Music21's\n",
    "objects always begin with a captial letter.  So the `Note` object\n",
    "is found in the `note` module.  The distinction between uppercase\n",
    "and lowercase is crucial to Python: if you type the name of an\n",
    "object with the wrong case it won't know what to do and won't\n",
    "give you any help in distinguishing between them."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the `note` module, there are other classes besides `note.Note`.\n",
    "The most important one is `note.Rest`, which as you imagine\n",
    "represents a rest.  If we load music21 with the command:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from music21 import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "then you'll now be able to access the `note` module just by\n",
    "typing `note` at any command line."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    ">>> note # doctest: +SKIP\n",
    "<module 'music21.note' from 'D:\\music21files\\music21\\note.pyc'>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you get something like this you'll have access to the music21\n",
    "`note` module any time you type `\"note\"`.  The filename after\n",
    "\"from 'D:\\\\music21files...'\" will differ for you.  It will show\n",
    "you where you have music21 installed (if you ever forget where\n",
    "you have music21 installed, this is an easy way to figure it out).  \n",
    "As long as it ends in `note.pyc` or `note.py` or something like that\n",
    "you're fine."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to know what else the `note` module contains besides\n",
    "the Note and Rest objects you can type \"`dir(note)`\" to find out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['GeneralNote',\n",
       " 'Lyric',\n",
       " 'LyricException',\n",
       " 'NotRest',\n",
       " 'NotRestException',\n",
       " 'Note',\n",
       " 'NoteException',\n",
       " 'Optional',\n",
       " 'Rest',\n",
       " 'SpacerRest',\n",
       " 'Test',\n",
       " 'TestExternal',\n",
       " 'Unpitched',\n",
       " '_DOC_ORDER',\n",
       " '_MOD',\n",
       " '__builtins__',\n",
       " '__cached__',\n",
       " '__doc__',\n",
       " '__file__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__spec__',\n",
       " 'base',\n",
       " 'beam',\n",
       " 'common',\n",
       " 'copy',\n",
       " 'duration',\n",
       " 'environLocal',\n",
       " 'environment',\n",
       " 'exceptions21',\n",
       " 'expressions',\n",
       " 'interval',\n",
       " 'noteheadTypeNames',\n",
       " 'pitch',\n",
       " 'prebase',\n",
       " 'stemDirectionNames',\n",
       " 'style',\n",
       " 'tie',\n",
       " 'unittest',\n",
       " 'volume']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(note)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some of the things in this list are classes of notes -- they are capitalized. Other classes are things that we'll get to later, like\n",
    ":class:`~music21.note.Lyric` objects.\n",
    "(By the way: I'm highlighting the names of\n",
    "most objects so they become links to the full documentation for the object. You \n",
    "can read it later when you're curious, frustrated, or *Mad Men* is a re-run; you\n",
    "certainly don't need to click them now)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### (Advanced digression):"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "   If you're more of a Python guru and you're afraid of \"polluting your namespace,\"\n",
    "   instead of typing \"`from music21 import *`\" you can type:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import music21"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "in which case instead of using the word `note`, you'll need to call it `music21.note`"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    ">>> music21.note # doctest: +SKIP\n",
    "<module 'music21.note' from 'D:\\music21files\\music21\\note.pyc'>\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you are a Python guru, you already knew that.  Probably if you didn't already \n",
    "know that, but you've heard about \"polluting your namespace,\" you have a Python\n",
    "guru friend who has screamed, \"Never use `import *`!\"  Trust me for now that\n",
    "this tutorial will be easier if you ignore your friend for a bit; by the end of\n",
    "it you'll know enough to be able to follow whatever advice seems most natural to\n",
    "you."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*(Back from the Python digression and especially the digression of the digression):* "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Okay, so now you now enough about modules and\n",
    "objects.  Let's create a `note.Note` object.  How about the F at the top of the\n",
    "treble clef staff:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = note.Note(\"F5\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use the convention where middle-C is C4, the octave above it is C5, etc."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now you have a Note.  Where is it?  It's stored in the variable `f`.  You can\n",
    "verify this just by typing `f`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.note.Note F>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And you can see that it's actually an F and actually in octave 5 by requesting the\n",
    "`.name` and `.octave` attributes on the `Note` object, `f`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'F'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.octave"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And there's an attribute called `.pitch` which returns another object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.pitch.Pitch F5>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.pitch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Well, that didn't tell you anything you didn't know already! Let's look at\n",
    "some other attributes that might tell you something you didn't know.  Some of them are sub-attributes, meaning they take two dots. Here's a sub-attribute on `pitch`, which we just said was itself an object, called `.frequency`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "698.456462866008"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.pitch.frequency"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And another sub-attribute called `pitch.pitchClassString`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.pitch.pitchClassString"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's a bit better! So an f is about 698hz (if A4 = 440hz), and it is pitch\n",
    "class 5 (where C = 0, C# and Db = 1, etc.)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A couple of things that you'll notice:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Your `frequency` probably has a bunch more numbers instead of ending with\n",
    "    \"...\".  Mine gives me \"698.456462866008\".  In the docs, we'll sometimes\n",
    "    write \"...\" instead of putting in all those numbers (or long strings); it's\n",
    "    partly a way of saving space, and also because the length of a long number\n",
    "    and even the last few digits will differ from computer to computer depending\n",
    "    on whether it's 32-bit or 64-bit, Mac or PC, number of sunspots last Autumn, etc.\n",
    "    Since I don't know what computer you're using, don't worry if you get slightly\n",
    "    different results.\n",
    "    \n",
    "2. There are single quotes around some of the output (like the `'F'` in `f.name`) and\n",
    "    none around others (like the `5` in `f.octave`).  The quotes mean that that attribute\n",
    "    is returning a String (a bunch of letters or numbers or simple symbols).\n",
    "    The lack of quotes means that it's returning a number (either an integer\n",
    "    or if there's a decimal point, a sneakingly decimal-like thingy called a\n",
    "    `float` (or \"floating-point number\") which looks and acts just like a decimal,\n",
    "    except when it doesn't, which is never when you'd expect. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "   *(The history\n",
    "        and theory behind* `floats` *will be explained to you at length by any\n",
    "        computer scientist, usually when he or she is\n",
    "        the only thing standing between you and the bar at a party. Really, we \n",
    "        shouldn't be using them anymore, except for the fact that for our computers \n",
    "        they're so much faster to work with than decimals.)*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The difference between the string `'5'` and\n",
    "    the number `5` is essential to keep in mind.  In Python (like most \n",
    "    modern programming languages) we use two equal signs (`==`) to ask if\n",
    "    two things are equal.  So:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.octave == 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's what we'd expect.  But try:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.pitch.pitchClassString == 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's because `5 == '5'` is `False`.  (There are some lovely languages\n",
    "    such as JavaScript and Perl where it's `True`; Python's not one of them.\n",
    "    This has many disadvantages at first, but as you go on, you might see\n",
    "    this as an advantage).  So to see if `f.pitchClassString` is `'5'` we need\n",
    "    to make `'5'` a string by putting it in quotes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.pitch.pitchClassString == \"5\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Python it doesn't matter if you put the `5` in single or double quotes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.pitch.pitchClassString == '5'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`pitchClassString` tells you that you should expect a string, because we've\n",
    "    put it in the name.  There's also a `.pitch.pitchClass` which returns a number:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.pitch.pitchClass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These two ways of getting a pitch class are basically the same for the note \"F\" \n",
    "    (except that one's a string\n",
    "    and the other is an integer) but for a B-flat, \n",
    "    which is `.pitchClass` 10 and `.pitchClassString` \"A\", it makes a difference."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's go ahead and make that B-flat note.  In `music21`, sharps are \"#\" as you\n",
    "might expect, but flats are \"-\".  That's because it's otherwise hard to tell the\n",
    "difference between the `Note` \"b\" (in this instance, you can write it in upper or\n",
    "lower case) and the symbol \"flat\".  So let's make that B-flat note:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "bflat = note.Note(\"B-2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I've called the variable \"`bflat`\" here.  You could call it \"`Bb`\" if you want or\n",
    "\"`b_flat`\", but not \"`b-flat`\" because dashes aren't allowed in variable names:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "tags": [
     "nbval-ignore-output",
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "can't assign to operator (<ipython-input-17-dff15d6dca04>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-17-dff15d6dca04>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    b-flat = note.Note(\"B-2\")\u001b[0m\n\u001b[0m                             ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to operator\n"
     ]
    }
   ],
   "source": [
    "b-flat = note.Note(\"B-2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since this note has an accidental you can get it by using the `.pitch.accidental` subproperty:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<accidental flat>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bflat.pitch.accidental"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we have something that isn't a number and doesn't have quotes around it.  That\n",
    "usually means that what `.accidental` returns is another object -- in this case an\n",
    ":class:`~music21.pitch.Accidental` object.  As we saw above, objects have attributes\n",
    "(and other goodies we'll get to in a second) and the `Accidental` object is no \n",
    "exception.  So let's make a new variable that will store `bflat`'s accidental:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc = bflat.pitch.accidental"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll get to all the attributes of `Accidental` objects in a bit, but here are two of\n",
    "them: `.alter` and `.displayLocation`.  You'll use the first one quite a bit: it shows\n",
    "how many semitones this `Accidental` changes the `Note`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.alter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since this `Accidental` is a flat, its `.alter` is a negative number.  Notice that it's also not an integer,\n",
    "but a float.  That might indicate that music21 supports things like quarter-tones,\n",
    "and in this case you'd be right.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Look back at the two lines \"`acc = bflat.pitch.accidental`\" \n",
    "and \"`acc.alter`\".  We set `acc` to be the value of `bflat.pitch`'s `.accidental` attribute\n",
    "and then we get the value of that variable's `.alter` attribute. We could have skipped the first step altogether and \"chained\" the\n",
    "two attributes together in one step:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bflat.pitch.accidental.alter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'normal'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.displayLocation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Good to know that we've set a sensible default.  If you want to have the accidental\n",
    "display above the note, you'll have to set that yourself:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'above'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.displayLocation = 'above'\n",
    "acc.displayLocation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our variable `\"acc\"` is the **exact** accidental that is attached to the B-flat\n",
    "Note stored as `bflat`.  It's not a flat that's similar to B-flat's flat, but\n",
    "the same one. (in computer-speak, `acc` is a *reference* to `.accidental`). So\n",
    "now if we look at the `.displayLocation` of `bflat.pitch.accidental` we see that it\n",
    "too is set to the silly \"above\" position:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'above'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bflat.pitch.accidental.displayLocation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python is one of those cool computer languages where if an object doesn't have\n",
    "a particular attribute but you think it should, you can add it to the object\n",
    "(some people find that this makes objects messy, but I don't mind it).  For \n",
    "what I hope are obvious reasons, the `Note` object does not have an attribute called\n",
    "\"`wasWrittenByStockhausen`\".  So if you try to access it, you'll get an error:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "tags": [
     "nbval-ignore-output",
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Note' object has no attribute 'wasWrittenByStockhausen'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-25-fbb7070911f6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mbflat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwasWrittenByStockhausen\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'Note' object has no attribute 'wasWrittenByStockhausen'"
     ]
    }
   ],
   "source": [
    "bflat.wasWrittenByStockhausen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But if you set the value of that weird attribute, you can use it later:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "bflat.wasWrittenByStockhausen = True\n",
    "f.wasWrittenByStockhausen = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then you can write an “if” statement to see if this is True or not:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hope you're enjoying Sirius!\n"
     ]
    }
   ],
   "source": [
    "if bflat.wasWrittenByStockhausen == True:\n",
    "    print(\"Hope you're enjoying Sirius!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that in the last line above you will need to put the spaces before the “print” command; Python uses spaces to keep track of what is inside of an if statement (or lots of other things) and what isn’t. \n",
    "\n",
    "(If you don’t get the Stockhausen joke, see: <a href=\"http://en.wikipedia.org/wiki/Karlheinz_Stockhausen#Sirius_star_system\">wikipedia</a> . )\n",
    "\n",
    "Nothing will print for the note f since we set .wasWrittenByStockhausen to False:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "if f.wasWrittenByStockhausen == True:\n",
    "    print(\"I love Helicopters!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At this point you might be tired of all this programming and just want to see or play your damn note! If you’ve installed a MusicXML reader such as MuseScore, Finale, Sibelius, or Finale Notepad, you can type:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQYAAABiCAYAAACyJ33EAAAACXBIWXMAAB7CAAAewgFu0HU+AAANmklEQVR4nO2dedQVdRnHPy+8gCAIAoIrouJa4hauZS6puXEwT2ZaZqallIq51gmtjlEeSdTUXPJYaidbDCytzFAjRdMyzAVTFMoFxQUQX1nl9sczc2bu3FnuMr+Zy/t+P+fMmbm/eeb3e16489zf8vyeB4rnIOBNoAvYqYT2hRBtxheA1UDFO54B+peqkRCiVI4HPiAwCv4xpUylhBDlMQ5YSa1RqHjlO5SnmhCiDAYDLxFvFPxjemnaCSFKYRrpRqECrAV2LUtBIUSxbAusItswVIBflaSjEKJgbiHbICz2zquBTctR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F8uCPao07YQQmfwfDgObHuGWsTgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 49,
       "width": 131
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "f.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and see it. We make the default note length a quarter-note.  We'll get to\n",
    "other note lengths in a minute.  Notice that we put in a sensible clef\n",
    "also, since otherwise you won't know that this note really is `F5`.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to hear it instead (and you're\n",
    "on Windows or Unix or an older-Mac (10.5 or older)) type:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "tags": [
     "nbval-ignore-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "                <div id='midiPlayerDiv55'></div>\n",
       "                <link rel=\"stylesheet\" href=\"//cuthbertLab.github.io/music21j/css/m21.css\"\n",
       "                    type=\"text/css\" />\n",
       "                <script>\n",
       "                require.config({\n",
       "                    paths: {'music21': '//cuthbertLab.github.io/music21j/src/music21'}\n",
       "                });\n",
       "                require(['music21'], function() {\n",
       "                               mp = new music21.miditools.MidiPlayer();\n",
       "                               mp.addPlayer('#midiPlayerDiv55');\n",
       "                               mp.base64Load('data:audio/midi;base64,TVRoZAAAAAYAAQABBABNVHJrAAAAFwD/AwAA4ABAiACQTVqIAIBNAIgA/y8A');\n",
       "                        });\n",
       "                </script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "f.show('midi') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You may need to wait a few seconds when hitting play if you're reading these docs online since the \"grand piano\" sound has to load and that's about a megabyte long.\n",
    "\n",
    "Maddeningly, Apple removed MIDI support in the version of QuickTime\n",
    "(QuickTime X) included in OS X 10.6 (Snow Leopard) and above (including\n",
    "Mountain Lion), so you'll need to get the older QuickTime 7 from <a href=\"http://support.apple.com/kb/DL923\">appleQuicktime</a> to\n",
    "make that work."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When we typed `f.octave` we didn't put any parentheses after it, but when we\n",
    "call `f.show()` we always need to put parentheses after it, even if there's\n",
    "nothing in them (in which case, we'll use the default `.show` format, which\n",
    "is usually `musicxml`)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`.show()` is what’s called a method on the `Note` object, while `.octave` is an attribute. Think of methods as like verbs (“O Note: show thyself!”) while attributes are like adjectives that describe the object. All methods need to have parentheses after them and inside the parentheses you can usually put other things (“parameters”) that control how to perform the action. For instance, let’s create a new note, `D` by transposing our B-flat up a major-third (“M3”):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.note.Note D>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = bflat.transpose(\"M3\")\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.note.Note B->"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bflat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instead of changing the original note, the `transpose()` method\n",
    "\"returns\" (that is, spits out) a new `note.Note`\n",
    "object that represents the operation of transposing it up (or down if you\n",
    "want to try \"-M3\") a certain interval. \n",
    "\n",
    "If you want to change `bflat` itself, you can add \"inPlace = True\" to the\n",
    "parameters of `.transpose()` separating it from the interval by a comma.\n",
    "Let's take it up a perfect fourth:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.note.Note E->"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bflat.transpose(\"P4\", inPlace=True)\n",
    "bflat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Of course now `bflat` is a terrible name for our variable!  You could type\n",
    "\"`eflat = bflat`\" and now you can call the note `eflat`.  But you'll probably not\n",
    "need to do this too often.  By the way, music21 handles some pretty wacky intervals,\n",
    "so if we go back to our variable `d` (which is still a `d` -- transposing bflat\n",
    "in place didn't change it; they're not connected anymore, barely on speaking terms\n",
    "even), let's transpose it up a doubly-diminished sixth:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<music21.note.Note B--->"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "whatNoteIsThis = d.transpose('dd6')\n",
    "whatNoteIsThis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "B-triple-flat! Haven't seen one of those in a <a href=\"http://www.music.vt.edu/musicdictionary/textt/Tripleflat.html\"> while</a>! Let's check that note's\n",
    "`.pitch.accidental.alter` and its `.pitch.accidental.name`.  These are sub-sub-properties, meaning that they have three dots in them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-3.0"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "whatNoteIsThis.pitch.accidental.alter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'triple-flat'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "whatNoteIsThis.pitch.accidental.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One last thing: not every note has an accidental.  The `d` for instance doesn't\n",
    "have one, so it returns `None`, which is a special value that puts nothing on the \n",
    "output. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.pitch.accidental"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to be sure that it is `None`, you can print the value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(d.pitch.accidental)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since `d.accidental` is `None` does this mean that `d.accidental.name` is `None` too?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'name'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-39-3f58fa9c100b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpitch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maccidental\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'name'"
     ]
    }
   ],
   "source": [
    "d.pitch.accidental.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nope! In fact it creates an error (which we'll also call \"raising an Exception\" \n",
    "for reasons that will become clear soon).  That's because instead of \n",
    "getting an `Accidental` object from `.accidental` like we did before, we got a\n",
    "`NoneType` object (i.e., `None`).  `Accidental` objects have an attribute called `name`, \n",
    "but the object `None` doesn't (it's like trying `.wasWrittenByStockhausen` before you've\n",
    "defined it as an attribute).  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you're just typing in IDLE\n",
    "or the command line, raising an Exception is no big deal, but when you're running\n",
    "a program, Exceptions will usually cause the program to crash (i.e., stop working).\n",
    "So we try to make sure that our `Notes` actually have `Accidentals` before we \n",
    "print the `.accidental`'s name, and we do that by using another `if` statement:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.pitch.accidental is not None:\n",
    "    print(d.pitch.accidental.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This way is safer because we will only try to print `d.pitch.accidental.name` if \n",
    "`d.pitch.accidental` is not `None`.  Since it *is* `None` in this case, Python will\n",
    "never try the second line (which would otherwise cause it to crash).\n",
    "\n",
    "If for some reason `d` did not have `.pitch`, we would need to test to see if that was `None` before checking the subproperty to see if it had an `.pitch.accidental`.\n",
    "\n",
    "This might be a good place to take a rest for a second.  So make a `Rest`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = note.Rest()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Be sure to put the \"()\" (double parentheses) signs after `note.Rest` otherwise\n",
    "strange things will happen (technically you get a reference to the class `note.Rest`,\n",
    "which will come in handy in about 10 chapters, but not right now).\n",
    "\n",
    "You can `.show()` it as a '`musicxml`' file of course... "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQwAAABiCAYAAAClBe0NAAAACXBIWXMAAB7CAAAewgFu0HU+AAAM00lEQVR4nO3deZAcZRnH8e9mN4RAQgIJCQ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HRiUjd/g+1U9kapBjLTQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 49,
       "width": 134
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "r.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "...but if you try to hear it \n",
    "as a '`midi`' file, don't expect to be overwhelmed. \n",
    "\n",
    "A `Rest` is an object type that does not have `.pitch` on it, so naturally it doesn't have `.pitch.accidental` either:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Rest' object has no attribute 'pitch'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-43-38156eec5526>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpitch\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'Rest' object has no attribute 'pitch'"
     ]
    }
   ],
   "source": [
    "r.pitch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One last thing: notice that we never used a variable name called \"`note`\" to\n",
    "store a `note.Note` object.  **Don't do this**.  If you type something like this\n",
    "(don't type this if you want to continue typing along with the user guide):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "#_DOCS_SHOW note = note.Note(\"C#3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Well now you're in a bind.  You've got your `Note` object stored as `note`, but\n",
    "we *need* the `note` *module* in order to create new `Note` objects and now you have\n",
    "no way of getting it. (this is the problem that \"polluting the namespace\" causes\n",
    "that your Python guru friend might have warned you about).  So unless you're\n",
    "_Amadeus's_ Emperor Joseph who complained that there were \"too many notes,\"\n",
    "you're probably going to want to make more `note.Note` objects in the future,\n",
    "so don't use `note` as a variable name. (The same goes with `pitch`, `scale`,\n",
    "`key`, `clef`, and so on.  You'll see me use variable names like `myNote` and\n",
    "`myClef` to avoid the problem)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "sphinx_links": {
     "any": true
    }
   },
   "source": [
    "Okay, now you have the basics of `Note` objects down, let's go on to :ref:`Chapter 3: Pitches and Durations <usersGuide_03_pitches>`."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
